<template>
  <div id="changePasswordPlus">
    <div id="changePassword">
      <h1>司机修改密码</h1>
      <el-form
              :label-position="labelPosition"
              label-width="100px"
              :model="changePasswordForm"
      >
        <el-form-item label="原密码:">
          <el-input
                  v-model="changePasswordForm.oldpwd"
                  placeholder="输入原密码"
                  @blur="oldpwdalready"
                  type="password"
                  show-password
          >
          </el-input>
          <span class="red">
            {{ oldpwderr }}
          </span>
        </el-form-item>
        <el-form-item label="新密码:">
          <el-input
                  v-model="changePasswordForm.newpwd"
                  placeholder="输入新密码"
                  class="inputkuang"
                  type="password"
                  show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码:">
          <el-input
                  v-model="changePasswordForm.newpwdAgain"
                  placeholder="再次输入新密码"
                  @blur="newAndNewAgain"
                  type="password"
                  show-password
          ></el-input>
          <span class="red">
            {{ querenpwderr }}
          </span>
        </el-form-item>
        <div id="anniu">
          <el-button @click="quxiao">取消</el-button>
          <el-button @click="changeSucss" type="primary">确定</el-button>
        </div>

      </el-form>
    </div>
  </div>
</template>

<script>
  import * as api from "../../utils/api";
  export default {
    name: "userchangepwd",
    data() {
      return {
        user_id: "",
        labelPosition: "right",
        changePasswordForm: {
          userId: "",
          oldpwd: "",
          newpwd: "",
          newpwdAgain: "",
        },
        oldPwdRight: false,
        newANDnewagain: false,
        pwdstate: false,
        oldpwderr: "",
        querenpwderr: "",
      };
    },
    methods: {
      //确认密码框失焦
      newAndNewAgain() {
        this.querenpwderr = "";
        if (
                this.changePasswordForm.newpwd === this.changePasswordForm.newpwdAgain
        ) {
          this.newANDnewagain = true;
        } else {
          this.querenpwderr = "确认密码不一致!";
        }
      },

      //原密码框失焦:
      oldpwdalready() {
        this.oldpwderr = ""; //
        if (this.changePasswordForm.oldpwd == "") {
          this.$message("你没有输入原密码!");
        } else {
          this.axios({
            url: api.GET_OLD_PWD,
            method: "get",
            params: {
              user_id: parseInt(this.user_id),
              user_pwd: this.changePasswordForm.oldpwd,
            },
          }).then((data) => {
            if (data.data.code == 200) {
              this.oldPwdRight = true;
            } else {
              this.oldpwderr = "*" + data.data.msg;
            }
          });
        }
      },
      //确定按钮
      changeSucss() {
        if (
                this.changePasswordForm.oldpwd == "" ||
                this.changePasswordForm.newpwd == "" ||
                this.changePasswordForm.newpwdAgain == ""
        ) {
          this.$message.error("没有填写完整");
        } else {
          if (this.oldPwdRight == true && this.newANDnewagain == true) {
            this.axios({
              url: api.POST_CHANGE_PWD,
              method: "post",
              params: {
                user_id: parseInt(this.user_id),
                user_pwd: this.changePasswordForm.newpwd,
              },
            }).then((data) => {
              // console.log(data);
              if (data.data.code == 200) {
                this.$message.success(data.data.msg);
                this.changePasswordForm = {
                  userId: "",
                  oldpwd: "",
                  newpwd: "",
                  newpwdAgain: "",
                };
                this.querenpwderr = "";
                this.oldpwderr = "";
                this.$router.push("/login");
              } else {
                this.$message.error(data.data.msg);
              }
            });
          }
        }
      },
      //取消按钮
      quxiao() {
        this.querenpwderr = "";
        this.oldpwderr = "";
        this.changePasswordForm = {
          userId: "",
          oldpwd: "",
          newpwd: "",
          newpwdAgain: "",
        };
      },
      //获取当前登录者
      getUser_id() {
        this.user_id = JSON.parse(sessionStorage.getItem("user")).user_id;
        // console.log(this.user_id);
      },
    },
    created() {
      this.getUser_id();
    },
  };
</script>

<style lang = "less" scoped>
  #changePassword {
    width: 45rem;
    height: 30rem;
    padding: 4rem;
    margin: auto;
    background-color: #53617B;
    border-radius: 1rem;
    h1 {
      color: white;
      font-size: 2rem;
      text-align: center;
      margin-bottom: 3rem;
    }
  }
  /deep/.el-form-item__label {
    color: white;
    /*font-weight: bolder;*/
    font-size: 1.4rem;
  }
  /deep/.el-form-item__content {
    width: 40rem;
  }
  /deep/.el-input {
    background-color: rgba(0, 0, 0, 0);
    border: 0;
    border-bottom: 1px solid white;
    border-radius: 0;
    height: 3rem;
    font-size: 1.4rem;
    width: 30rem;

    &__inner {
      background-color: rgba(0, 0, 0, 0);
      border: 0;
      color: white;
    }
  }
  .red {
    color: red;
  }
  #changePasswordPlus {
    box-sizing: border-box;
    padding-top: 10rem;
    /*padding: 10rem 0;*/
    width: 100%;
    /*background-size: 100% 100%;*/
    /*background-color: #;*/
    /*background-image: url("../../assets/bg.jpg");*/
  }
  #anniu{
    width: 100%;
    height: 10rem;
    text-align: center;
  }
  #anniu>.el-button{
    margin: 2rem 3rem;
  }
  .el-form .el-form-item__label{
    text-align: left!important;
    margin-left: 0;
  }
</style>